﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules.Person
{
    public static partial class PersonExceptions
    {
        /// <summary>
        /// 根据出生日期获取年龄
        /// </summary>
        /// <param name="birthday"></param>
        /// <returns></returns>
        public static int GetAge(this DateTime birthday)
        {
            // 计算当前日期
            DateTime today = DateTime.Today;
            // 根据日期计算年龄
            int age = today.Year - birthday.Year;
            // 如果当前日期还未达到出生日期的生日，则年龄需要减一
            if (today.Month < birthday.Month || (today.Month == birthday.Month && today.Day < birthday.Day))
            {
                age--;
            }
            return age;
        }
    }
}
